package study.ct.com.retrofit_rxjava.net.retrofit;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by ct on 2017/10/11 0011.
 * interceptor  拦截器
 */

public class NetInterceptor implements Interceptor {

    private RequestHandler handler;

    public NetInterceptor(RequestHandler handler){
        this.handler=handler;
    }

    @Override
    public Response intercept(Chain chain) throws IOException {

        Request request=chain.request();
        if (handler!=null){
            request=handler.onBeforeRequest(request,chain);
        }

        Response response = chain.proceed(request);
        if (handler!=null){
            Response tmp = handler.onAfterRequest(response, chain);
            if (tmp!=null){
                return tmp;
            }
        }

        return response;
    }
}
